#include "Explosion.h"

float i=0.0;

Explosion::Explosion() : Particle()
{
	this->texture = new Texture();
	this->texture->load("explossion/explossion.tga");
	this->time_life=15.0;
	this->active=false;
	this->size=0.0;
	this->maxSize=12.0;
	this->model.setIdentity();
}

Explosion::~Explosion(void)
{
}


Explosion* Explosion::getInstance(){
	if (instance == NULL)
		instance = new Explosion();
	return instance;
}

void Explosion::render()
{
	
	if(!active) return;

	for( int i=0; i<3; i++ ) 
	    for( int j=0; j<3; j++ ) {
		if ( i==j )
			model.m[i*4+j] = 1.0;
		else
		    model.m[i*4+j] = 0.0;
	    }
	//std::cout << "OLA K ASE" << this->position.x << this->position.y << this->position.z<< std::endl;
	glPushMatrix();
	model.set();
	model.setTranslation(this->position.x,this->position.y,this->position.z);
	glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );  
	texture->bind();
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE);
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); 
	glDepthMask(GL_FALSE);
	glBegin(GL_QUADS); 
	glVertex2f(0.0,0.0);glTexCoord2f(0.0,0.0);	
	glVertex2f(size,0.0);glTexCoord2f(1.0,0.0);	
	glVertex2f(size,size);glTexCoord2f(1.0,1.0);
	glVertex2f(0.0,size);glTexCoord2f(0.0,1.0);
	glEnd();
	texture->unbind();
	glDisable(GL_BLEND);
	glDepthMask(GL_TRUE);
	glPopMatrix();	

	//std::cout << "puta bola de los huevos" << std::endl;
	//glPushMatrix();
	//glEnable(GL_BLEND);
	//glBlendFunc(GL_SRC_ALPHA, GL_ONE);
	////glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); 
	//glDepthMask(GL_FALSE);
	//glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );  
	//this->model.set();
	//this->model.setTranslation(100,100,100);
	//glutSolidSphere(150,105,105);
	//glDisable(GL_BLEND);
	//glDepthMask(GL_TRUE);
	//glPopMatrix();
}

void Explosion::update(float seconds_elapsed)
{
	this->time_life-=seconds_elapsed;
	if(time_life<=0) active=false;	
	form();
}

void Explosion::setPos(Vector3 pos)
{
	this->position=pos;
	this->active=true;
}

void Explosion::form()
{
	float a;
	a=1/(0.5+i);
	if(a>=0) this->size=a;
	i+=0.05;
}

Explosion* Explosion::instance = NULL;